package cn.edu.sdjzu.cs.dingcan.servlet;

import cn.edu.sdjzu.cs.dingcan.service.CustomerService;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "CustomerServlet", urlPatterns = "/customer-register")
public class CustomerRegisterServlet extends HttpServlet {
    private final CustomerService customerService = new CustomerService();
    private static final String REGISTER_JSP_PATH = "app/detail/customer-register.jsp";
    private static final String REGISTER_SUCCESS_JSP_PATH = "app/detail/customer-register-success.jsp";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String> errors = new HashMap<>();
        request.setAttribute("errors", errors);
        request.getRequestDispatcher(REGISTER_JSP_PATH).forward(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String> errors = new HashMap<>();
        Map<String, String> formData = new HashMap<>();

        String username = request.getParameter("username");
        if(StringUtils.isBlank(username)) {
            errors.put("tbUsername", "请输入用户名");
        } else {
            try {
                if(this.customerService.usernameExist(username)) {
                    errors.put("tbUsername", "用户名已存在，请选择不同的用户名");
                }
            } catch (SQLException throwables) {
                throw new RuntimeException("执行数据库操作时遇到错误。", throwables);
            }
            formData.put("tbUsername", username);
        }

        String realName = request.getParameter("realName");
        if(StringUtils.isBlank(realName)) {
            errors.put("tbRealName", "请输入真实姓名");
        } else {
            formData.put("tbRealName", realName);
        }

        String password = request.getParameter("password");
        if(password == null) {
            errors.put("tbPassword", "请输入密码");
        } else if(password.length() < 6) {
            errors.put("tbPassword", "请使用6位字符以上的密码");
        }

        if(!errors.containsKey("tbPassword")) { // 当密码无错误时再检查确认密码
            String passwordConfirm = request.getParameter("passwordConfirm");
            if (password != null && !password.equals(passwordConfirm)) {
                errors.put("tbPasswordConfirm", "两次输入的密码不一致");
            }
        }

        String phoneNumber = request.getParameter("phoneNumber");
        if(StringUtils.isBlank(phoneNumber)) {
            errors.put("tbPhoneNumber", "请输入真实姓名");
        } else {
            formData.put("tbPhoneNumber", phoneNumber);
        }

        String email = request.getParameter("email");
        if(StringUtils.isBlank(email)) {
            errors.put("tbEmail", "请输入邮箱");
        } else {
            formData.put("tbEmail", email);
        }

        String deliveryAddress = request.getParameter("deliveryAddress");
        if(StringUtils.isBlank(deliveryAddress)) {
            errors.put("tbDeliveryAddress", "请输入配送地址");
        } else {
            formData.put("tbDeliveryAddress", deliveryAddress);
        }
        request.setAttribute("errors", errors);
        request.setAttribute("formData", formData);

        if(errors.size() > 0) {
            request.getRequestDispatcher(REGISTER_JSP_PATH).forward(request, response);
        } else {
            try {
                this.customerService.customerRegister(username, realName, phoneNumber, email, deliveryAddress, password);
                request.getRequestDispatcher(REGISTER_SUCCESS_JSP_PATH).forward(request, response);
            } catch (SQLException throwables) {
                throw new RuntimeException("执行数据库操作时遇到错误。", throwables);
            }
        }
    }
}
